﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QunYing.Web.market
{
    public partial class warn : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            dt.Rows.Add("报警信息", "");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GVBind();
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void GVBind()
        {
            string userName = ((ArrayList)Session["userInfo"])[0].ToString();
            BLL.DeviceRealTimeInfo info = new BLL.DeviceRealTimeInfo();
            DateTime endDate = DateTime.MaxValue;
            DateTime startDate = Convert.ToDateTime("1900-1-1");
            DataTable dt = info.QueryWarn(userName, "", startDate, endDate);
            int intCount = dt.Rows.Count;
            bool b = true;
            if (intCount < 5)
            {
                b = false;
                for (int i = 0; i < 5 - intCount; i++)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                }
                GridView1.DataSource = dt;
            }
            else
            {
                DataTable dt2 = dt.Clone();
                for (int i = 0; i < 5; i++)
                {
                    dt2.ImportRow(dt.Rows[i]);
                }
                GridView1.DataSource = dt2;
            }
            GridView1.DataBind();

            if (!b)
            {
                for (int i = intCount; i < GridView1.Rows.Count; i++)
                {
                    LinkButton btn = (LinkButton)GridView1.Rows[i].FindControl("btnDetail");
                    btn.Visible = false;
                }
            }

        }

        protected void btnDetail_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32((sender as LinkButton).CommandArgument);
            string strIcsa = this.GridView1.DataKeys[id - 1].Values[1].ToString();//ICSA编号
            string strTime = this.GridView1.DataKeys[id - 1].Values[2].ToString();//告警时间
            Session["icsa"] = strIcsa;
            Session["time"] = Convert.ToDateTime(strTime).ToString("yyyy-MM-dd HH:mm:ss");
            Response.Redirect("~/water.aspx");
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i <= GridView1.Rows.Count; i++)
                {
                    if (e.Row.Cells[4].Text == "正报警")
                    {
                        e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
    }
}
